package com.sgolub.commanndsonline.web;
   
import java.security.Principal;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.context.request.WebRequest;

import com.sgolub.commanndsonline.domain.entity.AdvertisingRegistry;
import com.sgolub.commanndsonline.domain.entity.UserBasket;
import com.sgolub.commanndsonline.domain.ref.UserType;
import com.sgolub.commanndsonline.domain.sec.SecRole;
import com.sgolub.commanndsonline.domain.sec.SecUser;
import com.sgolub.commanndsonline.helpers.MailSender;
import com.sgolub.commanndsonline.service.entity.AdvertisingRegistryService;
import com.sgolub.commanndsonline.service.entity.UserBasketService;
import com.sgolub.commanndsonline.service.sec.SecRoleService;
import com.sgolub.commanndsonline.service.sec.SecUserService;
 
@Controller
public class AppController { 
	 
    @Autowired
    private SecUserService secUserService;
    
    @Autowired
    private SecRoleService secRoleService;
    
    @Autowired
    private AdvertisingRegistryService advertisingRegistryService;
	
    @Autowired
    private UserBasketService userBasketService;
    
 
    @RequestMapping("/")
    public String home(WebRequest request) {
    	System.out.println("AppController - redirection to INDEX");
        return "redirect:/index";
    }    
         
           
    @RequestMapping("/index") 
    public String resolveUserType(Map<String, Object> map, WebRequest request, Principal principal) {
    	String currentLang = "";
    	//String requestLang = request.getParameter("language");
    	System.out.println("language = " + request.getParameter("language"));
    	if(request.getParameter("language") != null){
        	if (request.getParameter("language").equals("ru")){
        		currentLang = "_ru";
        	}    		
    	}
    	
    	SecUser currUser = secUserService.getByName(principal.getName()); 
    	String currentUserAUTHORITY = currUser.getSecRole().getAuthority();
    	System.out.println("AUTHORITY = " + currentUserAUTHORITY);
    	
    	if (currentUserAUTHORITY.equals("ROLE_ADMIN")) {
    		return "admin/adminMain"+currentLang;
    	} else if (currentUserAUTHORITY.equals("ROLE_USER")) {
    		return "user/userMain"+currentLang;
    	} else {
    		map.put("registration"+currentLang, new SecUser());
            return "registration"+currentLang;
    	}  
    } 
       
    @RequestMapping("/registration")
    public String registration(Map<String, Object> map) {

    	map.put("registration", new SecUser());
    System.out.println("AppController - registration");
    return "registration";
    
    
    }
     
    @RequestMapping("/registration_ru")
    public String registration_ru(Map<String, Object> map) {

    	map.put("registration_ru", new SecUser());
    System.out.println("AppController - registration_ru");
    return "registration_ru";
    
    
    }
    
    @RequestMapping(value="/addUser", method = RequestMethod.POST)
    public String addUser(WebRequest request) {
    	System.out.println("NAME = " + request.getParameter("name"));
    	System.out.println("UNAME = "+ request.getParameter("username"));
    	System.out.println("PASS = " + request.getParameter("password"));
    	System.out.println("COMPANY = " + request.getParameter("company"));
    	System.out.println("EMAIL + " + request.getParameter("email"));
    	System.out.println("TELEFON + " + request.getParameter("telefon"));
    	
    	SecUser newSecUser = new SecUser();
    	SecRole newSecRole = new SecRole();
    	UserType newSecUserType = new UserType();
    	newSecRole = secRoleService.get(3); //Simple User Role 
    	newSecUser.setName(request.getParameter("name"));
    	newSecUser.setUsername(request.getParameter("username"));
    	newSecUser.setPassword(request.getParameter("password"));
    	newSecUser.setCompany(request.getParameter("company"));
    	newSecUser.setEmail(request.getParameter("email"));
    	newSecUser.setTelefon(request.getParameter("telefon"));
    	newSecUser.setEnabled(0);
    	
    	newSecUserType.setId(3); //Simple client
    	newSecUser.setUserType(newSecUserType);
    	newSecUser.setSecRole(newSecRole);
    	secUserService.add(newSecUser);
    	
    	MailSender.newUserRegistered(secUserService.getByName(newSecUser.getUsername()));
    	
    System.out.println("AppController - addUser");
    return "redirect:/index";
    }    
    
     
    @RequestMapping("/admin/usersList") 
    public String reports(Map<String, Object> map) {
    System.out.println("AppController - usersList");
    map.put("user", 		new SecUser());
    map.put("usersList", 	secUserService.list());
    return "admin/usersList";
    }    
     
    @RequestMapping("/admin/orderListAdmin") 
    public String orderList(Map<String, Object> map) {
    System.out.println("AppController - orderListAdmin");
    return "admin/orderListAdmin";
    }    

    @RequestMapping("/user/advertisingRegistryList") 
    public String advertisingRegistryList(Map<String, Object> map) {
    System.out.println("AppController - advertisingRegistryList");
      
    map.put("advertisingRegistry", 		new AdvertisingRegistry());
    map.put("advertisingRegistryList", 	advertisingRegistryService.list());
    return "user/advertisingRegistryList";
    } 

    @RequestMapping("/user/basketList") 
    public String basketList(Map<String, Object> map, Principal principal) {
    System.out.println("AppController - basketList");
	if (principal == null) {
		return null;
	}
	System.out.println("Current User  = :" + principal.getName());
	
	Integer currUserId = secUserService.getByName(principal.getName()).getId(); 
	
    map.put("userBasketItem", 		new UserBasket());
    map.put("userBasketList", 	userBasketService.listCurrentUserBasket(currUserId));
    return "user/basketList";
    }     
    
    
    @RequestMapping("/user/orderList") 
    public String orderList(Map<String, Object> map, Principal principal) {
    System.out.println("AppController - basketList");
	if (principal == null) {
		return null;
	}
    return "user/orderList";
    }     
        
    
    
    @RequestMapping("/user/advertisingRegistryListV2") 
    public String advertisingRegistryListV2(Map<String, Object> map) {
    System.out.println("AppController - advertisingRegistryList");
     
    map.put("advertisingRegistry", 		new AdvertisingRegistry());
    map.put("advertisingRegistryList", 	advertisingRegistryService.list());
    return "user/advertisingRegistryListV2";
    }
    
    
    
    
    
    
    
    @RequestMapping("/unregisteredRegistryList") 
    public String unregisteredRegistryList(Map<String, Object> map, WebRequest request) {
    System.out.println("AppController - unregisteredRegistryList");
    
    System.out.println("== PASSED PARAMETERS ==");
    System.out.println("_pRegionName =" + request.getParameter("_pRegionName")); 
    System.out.println("_pLocalityName =" + request.getParameter("_pLocalityName"));
    System.out.println("_pSupportType =" + request.getParameter("_pSupportType"));
    System.out.println("_pLight =" + request.getParameter("_pLight"));
    System.out.println("_pAddresString =" + request.getParameter("_pAddresString"));
    System.out.println("_pPriceFrom =" + request.getParameter("_pPriceFrom"));
    System.out.println("_pPriceFrom =" + request.getParameter("_pPriceFrom"));
    
    
    map.put("unregisteredRegistry", 		new AdvertisingRegistry());
    map.put("unregisteredRegistryList", 	advertisingRegistryService.list());
    return "unregisteredRegistryList";
    }
        
    
//    @RequestMapping("/index2")
//    public String listContacts(Map<String, Object> map) {
//
//        map.put("contact", new Contact());
//        map.put("contactList", contactService.listContact());
//
//        return "contact";
//    } 
      
 

}